package com.bwie.bargain.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;


import cn.hutool.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bwie.bargain.entity.BargainDownRecordEntity;
import com.bwie.bargain.service.BargainDownRecordService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author lfw
 * @email sunlightcs@gmail.com
 * @date 2022-03-09 20:54:42
 */
@RestController
@RequestMapping("bargain/bargaindownrecord")
public class BargainDownRecordController {
    @Autowired
    private BargainDownRecordService bargainDownRecordService;


    /**
     * 跳转砍价页面
     */
    @RequestMapping("/toPage")
    public String toPage(@RequestParam("url")String url,HttpServletResponse response){
        R r=bargainDownRecordService.checkLink(url);
        if (r.get("code").equals(0)){
            response.setCharacterEncoding("utf-8");
            try {
                response.sendRedirect("http://127.0.0.1:5500/information.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "error";
    }


    /**
     * 帮砍链接
     */
    @RequestMapping("/generateLink")
    public R generateLink(){
        R r=bargainDownRecordService.makeLink();
        return r;
    }


    /**
     * 帮助砍价
     */
    @RequestMapping("/helpbargain")
    public R helpbargain(@RequestParam Map<String, Object> params){
        return bargainDownRecordService.helpbargain(params);
    }

    /**
     * 生成砍价活动记录表
     */
    @RequestMapping("/addrecord")
    public R addrecord(@RequestParam Map<String, Object> params){
        return bargainDownRecordService.addrecord(params);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bargainDownRecordService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		BargainDownRecordEntity bargainDownRecord = bargainDownRecordService.getById(id);

        return R.ok().put("bargainDownRecord", bargainDownRecord);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BargainDownRecordEntity bargainDownRecord){
		bargainDownRecordService.save(bargainDownRecord);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BargainDownRecordEntity bargainDownRecord){
		bargainDownRecordService.updateById(bargainDownRecord);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		bargainDownRecordService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
